/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package EvoAlg;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Geost1gma
 */
public class Main {

    int popsize = 30;                          // Population size
    double pcrossover = 0.9;                          // Crossover probability
    double pmutation = 0.1;        // Mutation probability
    double targetFitness = 1.0;           // Target fitness being sought
    int selection_mech = Algorithm.TOURNAMENT_SELECT;
    int adult_selector = SelectionObject.OVERPOPULATION_WITHOUT_DEAD_PARENTS;
    int targetfoundAtGeneration= -1;
    Izhikevich izhi = new Izhikevich();
    Algorithm alg;
    public int maxGenerasjoner = 50;
    

    double[] maxFitness = new double[100];
    double[] minFitness = new double[100];
    double[] avrFitness = new double[100];
    
    public void Izhikevich(){
        
        alg = new Algorithm(izhi, popsize, pcrossover, pmutation);
        izhi.setTargetFitness(1.0);

        izhi.EvaluateAll(alg.getPop());

        SelectionObject select = new SelectionObject(alg);

        double maxFitnes = 0;
        int teller = 0;
        while (teller < maxGenerasjoner) {

            if(adult_selector == SelectionObject.FULL_GENERATIONAL_SHIFT){
                select.fullGenerationalShift(selection_mech);
                alg.getProblem().EvaluateAll(alg.getPop());
            }else if(adult_selector == SelectionObject.OVERPOPULATION_WITH_DEAD_PARENTS){
                select.overpopulationAllParentsDie(selection_mech);
                alg.getProblem().EvaluateAll(alg.getPop());
            }else if(adult_selector == SelectionObject.OVERPOPULATION_WITHOUT_DEAD_PARENTS){
                select.overpopulationCompeteWithParents(selection_mech);
                alg.getProblem().EvaluateAll(alg.getPop());
            }

            maxFitnes = alg.findMaxFitness();



            maxFitness[teller] = alg.findMaxFitness();
            minFitness[teller] = alg.findMinFitness();
            avrFitness[teller] = alg.avg();

            if(maxFitnes >0.99 && targetfoundAtGeneration < 0) targetfoundAtGeneration = teller;

            
//            System.out.println(maxFitness[teller]);
//            System.out.println(alg.avg());
//            System.out.println(minFitness[teller] + "\n");

            teller++;
//            for (int i = 0; i <alg.getPopsize(); i++) {
//                System.out.println(alg.getPop().getIndividualAtIndex(i).getFitness());
//
//            }

        }

    }

    public void onemax() {

        /*
        OneMaxProblem problem = new OneMaxProblem();
        Algorithm alg = new Algorithm(problem, popsize, pcrossover, pmutation);

        problem.setGenotypeLength(genotypeLength);
        problem.setNumberOfPhenoTypes(numberOfPhenotypes);
        problem.setTargetFitness(targetFitness);

        problem.EvaluateAll(alg.getPop());

        SelectionObject sel = new SelectionObject(alg);

        double maxFitnes = 0;
        int teller = 0;
        while (teller < 100) {
            if(adult_selector == SelectionObject.FULL_GENERATIONAL_SHIFT){
                sel.fullGenerationalShift(selection_mech);
                alg.getProblem().EvaluateAll(alg.getPop());
            }else if(adult_selector == SelectionObject.OVERPOPULATION_WITH_DEAD_PARENTS){
                sel.overpopulationAllParentsDie(selection_mech);
                alg.getProblem().EvaluateAll(alg.getPop());
            }else if(adult_selector == SelectionObject.OVERPOPULATION_WITHOUT_DEAD_PARENTS){
                sel.overpopulationCompeteWithParents(selection_mech);
                alg.getProblem().EvaluateAll(alg.getPop());
            }

            maxFitnes = alg.findMaxFitness();

            maxFitness[teller] = alg.findMaxFitness();
            minFitness[teller] = alg.findMinFitness();
            avrFitness[teller] = alg.avg();
            if(maxFitnes >0.99 && targetfoundAtGeneration < 0) targetfoundAtGeneration = teller;
            teller++;
            System.out.println(maxFitnes);
        }
        //targetfoundAtGeneration = teller;
        //System.out.println(teller);
         *
         * 
         */

    }

    public void resetTables(){
        maxFitness = new double[100];
        minFitness = new double[100];
        avrFitness = new double[100];
    }


    public double getPcrossover() {
        return pcrossover;
    }

    public void setPcrossover(double pcrossover) {
        this.pcrossover = pcrossover;
    }

    public double getPmutation() {
        return pmutation;
    }

    public void setPmutation(double pmutation) {
        this.pmutation = pmutation;
    }

    public int getPopsize() {
        return popsize;
    }

    public void setPopsize(int popsize) {
        this.popsize = popsize;
    }

    public double getTargetFitness() {
        return targetFitness;
    }

    public void setTargetFitness(double targetFitness) {
        this.targetFitness = targetFitness;
    }

    public int getAdult_selector() {
        return adult_selector;
    }

    public void setAdult_selector(int adult_selector) {
        this.adult_selector = adult_selector;
    }

    public int getSelection_mech() {
        return selection_mech;
    }

    public void setSelection_mech(int selection_mech) {
        this.selection_mech = selection_mech;
    }

    public double[] getAvrFitness() {
        return avrFitness;
    }

    public void setAvrFitness(double[] avrFitness) {
        this.avrFitness = avrFitness;
    }

    public double[] getMaxFitness() {
        return maxFitness;
    }

    public void setMaxFitness(double[] maxFitness) {
        this.maxFitness = maxFitness;
    }

    public double[] getMinFitness() {
        return minFitness;
    }

    public void setMinFitness(double[] minFitness) {
        this.minFitness = minFitness;
    }

    public int getTargetfoundAtGeneration() {
        return targetfoundAtGeneration;
    }

    public void setTargetfoundAtGeneration(int targetfoundAtGeneration) {
        this.targetfoundAtGeneration = targetfoundAtGeneration;
    }

    public Algorithm getAlg() {
        return alg;
    }

    public void setAlg(Algorithm alg) {
        this.alg = alg;
    }

    public Izhikevich getIzhi() {
        return izhi;
    }

    public void setIzhi(Izhikevich izhi) {
        this.izhi = izhi;
    }

    
    public static void main(String[] args) {
        Main m = new Main();
        m.Izhikevich();
    }

    public int getMaxGenerasjoner() {
        return maxGenerasjoner;
    }

    public void setMaxGenerasjoner(int maxGenerasjoner) {
        this.maxGenerasjoner = maxGenerasjoner;
    }
}
